package com.maimao.goods.controller;

import com.maimao.api.mall.goods.SpecDetailApi;
import com.maimao.core.constant.Constants;
import com.maimao.core.model.resp.Result;
import com.maimao.core.utils.MaiMaoStringUtil;
import com.maimao.goods.controller.base.BaseController;
import com.maimao.goods.service.SpecDetailService;
import com.maimao.model.dynamicform.DynamicForm;
import com.maimao.model.dynamicform.vo.SpecDynamicFormVo;
import com.maimao.model.goods.SpecDetails;
import com.maimao.model.goods.enums.FormType;
import com.maimao.model.goods.input.SpecDetailForm;
import com.maimao.model.goods.vo.SpecDetailVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Collections;
import java.util.List;

/**
 * @author MaoLin Wang
 * @date 2020/12/11 8:15 下午
 */
@RestController
public class SpecDetailController extends BaseController implements SpecDetailApi {
    @Autowired
    private SpecDetailService specDetailService;

    /**
     * 根据规格组查询细分规格
     *
     * @param groupId
     * @return
     */
    @GetMapping("/by-group/{groupId}")
    @Override
    public Result<?> selectDetailByGroupId(@PathVariable("groupId") Integer groupId) {
        if (groupId == null || groupId < 1) {
            return Result.notValid();
        }
        return Result.okData(specDetailService.selectDetailByGroupId(groupId));
    }

    /**
     * 修改规格
     */
    @Override
    @PostMapping("/update-detail")
    public Result<?> updateSpecDetail(@Valid @RequestBody SpecDetailForm specDetailForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        Result<Void> res = checkAndInitSpec(specDetailForm, true);
        if (res != null) {
            return res;
        }
        specDetailService.update(specDetailForm);
        return Result.ok();
    }

    @Override
    @PostMapping("/add-detail")
    public Result<?> addSpecDetail(@Valid @RequestBody SpecDetailForm specDetailForm, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            return entityInvalid(bindingResult);
        }
        Result<Void> res = checkAndInitSpec(specDetailForm, false);
        if (res != null) {
            return res;
        }
        specDetailService.add(specDetailForm);
        return Result.ok();
    }

    /**
     * 根据三级类目id生成动态表单所需规格项
     *
     * @param categoryId 三级分类id
     */
    @Override
    @GetMapping("/loadDynamicForm")
    public Result<?> loadDynamicForm(@RequestParam("cid") Integer categoryId) {
        if (categoryId == null || categoryId < 1) {
            return Result.notValid();
        }
        SpecDynamicFormVo vo = specDetailService.loadDynamicForm(categoryId);
        return Result.okData(vo);
    }

    @Override
    @GetMapping("/client/paramOfsearch")
    public Result<?> findSearchSpecByCid(@RequestParam("cid") Integer categoryId) {
        if (categoryId == null || categoryId < 1) {
            return Result.notValid();
        }
        return Result.okData(specDetailService.findSearchSpecByCid(categoryId));
    }

    @Override
    @PostMapping("/client/paramOfsearchBatch")
    public Result<?> findSearchSpecByCidBatch(List<Integer> categoryIdList) {
        if (categoryIdList == null || categoryIdList.size() < 0) {
            return Result.notValid();
        }
        return Result.okData(specDetailService.findSearchSpecByCidList(categoryIdList));
    }

    private Result<Void> checkAndInitSpec(SpecDetailForm spec, boolean update) {
        if (spec.getId() == null || spec.getId() < 1) {
            if (update) {
                return Result.notValid();
            }
        }
        if (spec.getNumberic() && spec.getSearchable().equals(Constants.STATUS_1)) {
            if (spec.getSearchOptions() == null || spec.getSearchOptions().size() <= 0) {
                return Result.notValid("请输入至少一个搜索项");
            }
//            String sos = StringUtils.join(searchOptions, Constants.COMMA);
            String optionsStr = MaiMaoStringUtil.join2String(spec.getSearchOptions(), Constants.COMMA);
            spec.setSearchOptional(optionsStr);

        } else {
            spec.setSearchOptional(Constants.EMPTY);
        }
        if (spec.getNumberic()) {
            if (StringUtils.isEmpty(spec.getUnit())) {
                return Result.notValid("请填写数字规格单位");
            }
            if (!spec.getFormType().equals(FormType.NUMBER.getName())) {
                return Result.notValid("数字类型表单只能为数字输入框");
            }
        } else {
            spec.setUnit(Constants.EMPTY);
            if (spec.getFormType().equals(FormType.NUMBER.getName())) {
                return Result.notValid("非数字类型表单不可使用数字输入框");
            }
        }
        String formType = spec.getFormType();
        if (formType.equals(FormType.SELECT.getName()) || formType.equals(FormType.MULTI_SELECT.getName())) {
            List<String> specOptions = spec.getSpecOptions();
            if (specOptions == null || specOptions.size() <= 0) {
                return Result.notValid("请填写至少一个表单项");
            }
            for (int i = 0; i < specOptions.size(); i++) {
                specOptions.set(i, specOptions.get(i).trim());
            }
        } else {
            spec.setSpecOptions(Collections.emptyList());
        }

        return null;
    }
}
